﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;
using l1cs.server.datatables;
using l1cs.server.model;
using l1cs.server.serverpackets;
using l1cs.server.templates;

namespace l1cs.server.clientpackets
{
    public class C_AddBuddy : ClientBasePacket
    {
        private const String _C_ADD_BUDDY = "[C] C_AddBuddy";
        private static Logger _log = Logger.getLogger( typeof( C_AddBuddy ).FullName );

        public C_AddBuddy( byte[] decrypt, ClientThread client )
            : base( decrypt )
        {
            L1PcInstance pc = client.getActiveChar();
            BuddyTable buddyTable = BuddyTable.getInstance();
            L1Buddy buddyList = buddyTable.getBuddyTable( pc.getId() );
            String charName = readS();

            if ( string.Compare( charName, pc.getName(), true ) == 0 )
            {
                return;
            }
            else if ( buddyList.containsName( charName ) )
            {
                pc.sendPackets( new S_ServerMessage( 1052, charName ) ); // %s
                // は既に登録されています。
                return;
            }

            L1CharName cn = CharacterTable.getInstance().getL1CharName( charName.ToLower() );
            if ( cn != null )
            {
                int objId = cn.getId();
                String name = cn.getName();
                buddyList.add( objId, name );
                buddyTable.addBuddy( pc.getId(), objId, name );
                return;
            }
            pc.sendPackets( new S_ServerMessage( 109, charName ) ); // %0という名前の人はいません。
        }

        override
        public String getType()
        {
            return _C_ADD_BUDDY;
        }
    }
}